package com.mud.mudcommune.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mud.mudcommune.R;
import com.mud.mudcommune.entry.TypeItemEntry;
import com.mud.mudcommune.utils.ACache;
import com.mud.mudcommune.utils.LogUtils;

import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by zhoujin on 2016/12/10.
 */

public class MySpinnerAdapter extends MyBaseAdapter {
    private static final String TAG = MySpinnerAdapter.class.getSimpleName();
    private Context context;
    private List<TypeItemEntry> list;
    private String typeName;
    private int[] clickedList;
    private ACache mCache;
    private int clickPosition;

    public MySpinnerAdapter(Context context, List list, String type) {
        super(list);
        this.context = context;
        this.list = list;
        this.typeName = type;
        clickedList = new int[list.size()];
        mCache = ACache.get(context);
    }

    public void setNotifyDataSetChanged(String typeName) {
        this.typeName = typeName;
        clickedList = new int[list.size()];
        this.notifyDataSetChanged();
    }


    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        TypeItemEntry typeItemEntry = list.get(position);
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.case_spinner_item, null);
            viewHolder = new ViewHolder(view);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.radioButton.setText(typeItemEntry.getName());
        String value = mCache.getAsString(typeName);
        if (value != null) {
            LogUtils.i(TAG, "typeName: " + typeName + ", " + value);
            clickPosition = Integer.parseInt(value);
        } else {
            clickPosition = 0;
        }
        clickedList[clickPosition] = 1;
        LogUtils.i(TAG, "position: " + position + "clickPosition: " + clickPosition + "clickedList[clickPosition]: " + clickedList[clickPosition]);
        if (position == clickPosition) {
            LogUtils.i(TAG, "position: " + position + "clickPosition: " + clickPosition + "clickedList[clickPosition]: " + clickedList[clickPosition]);
            if (clickedList[clickPosition] == 0) {
                viewHolder.radioButton.setBackgroundResource(R.drawable.case_spinner_while);
            } else if (clickedList[clickPosition] == 1) {
                clickedList[clickPosition] = 0;
                viewHolder.radioButton.setBackgroundResource(R.drawable.case_spinner_subcolor);
            }
        } else {
            viewHolder.radioButton.setBackgroundResource(R.drawable.case_spinner_while);
        }
        return view;
    }

    static class ViewHolder {
        TextView radioButton;

        public ViewHolder(View view) {
            this.radioButton = ButterKnife.findById(view, R.id.item_type);
            view.setTag(this);
        }
    }
}
